package cn.fsw.pethome.base.controller;

import cn.fsw.pethome.base.domain.dto.BinderDTO;
import cn.fsw.pethome.base.domain.dto.LoginDTO;
import cn.fsw.pethome.base.domain.dto.WechatDTO;
import cn.fsw.pethome.base.service.ILoginService;
import cn.fsw.pethome.util.AjaxResult;
import cn.fsw.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    //登录
    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDTO loginDTO){
        try {
            Map<String, Object> account = loginService.account(loginDTO);
            return AjaxResult.ajaxResult().setData(account);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    //微信登录
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDTO wechatDTO){
        try {
            return loginService.wechat(wechatDTO);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    //绑定
    @PostMapping("/binder")
    public AjaxResult binder(@RequestBody BinderDTO binderDTO){
        try {
            return loginService.binder(binderDTO);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
